package com.example.demofdfs.example.lock.readLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadLockTest {

	private int count = 10;
	
	Lock lock = new ThreadLock();
//	Lock lock = new ReentrantLock();

	public static void main(String[] args) {
		final ThreadLockTest t = new ThreadLockTest();
		new Thread(()->{
			t.test_1();
		}).start();
		new Thread(()->{
			t.test_1();
		}).start();
	}
	
	public void test_1() {
		while(true) {
			sleep(50);
			lock.lock();
			try {
				if(count <= 0) 
					return;
			
				System.out.println(Thread.currentThread().getName() + " : lock..." + count--);
			} finally {
				lock.unlock();
			}
		}
	}
	public void test_2() {
		while(true) {
			sleep(100);
//			lock.lock();
			if(count <= 0) 
				return;
			
			System.out.println(Thread.currentThread().getName() + " : release..." + count--);
			
//			lock.unlock();
		}
	}
	
	
	public static void sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
